<template>
    <div class="register-view">
        <!-- <h1>注册页面</h1> -->
         <div class="content-wrap">
            <div class="logo">
                <img src="@/assets/img/logo.png" alt="">
            </div>
            <div class="phone">
                <input type="tel" placeholder="请输入手机号" v-model="phone">
                <p class="promptMessage">{{promptMessage}}</p>
            </div>
            <div class="imgCaptcha">
                <input type="text" placeholder="图形验证码" v-model="imgCaptcha">
                <img :src="imgCaptchaUrl" @click="captcha" alt="">
            </div>
            <div class="phoneCaptcha">
                <input type="text" placeholder="手机验证码" v-model="phoneCaptcha">
                <button @click="getsms">获取验证码</button>
            </div>
            <div class="password">
                <input type="password" placeholder="密码" v-model="password">
            </div>
            <div class="password2">
                <input type="password" placeholder="确认密码" v-model="password2" @keyup="confirmPassword">
            </div>
            <div class="invitationCode">
                <input type="text" placeholder="邀请码" v-model="invitationCode">
            </div>
            <div class="isRead">
                <input type="checkbox" name="vehicle" v-model="isRead" />我已经阅读并同意
                <router-link :to="'/home'">《社汇圈协议》</router-link>
            </div>
            <div class="register">
                <button @click="register">注册</button>
            </div>
            <div class="login">
                <router-link :to="'/login'">已有账号，立即登陆</router-link>
            </div>
         </div>
        
        <!-- <my-footer></my-footer> -->
    </div>
</template>

<script>
    import axios from 'axios'
    export default {
        name: 'RegisterView',
        data () {
            return {
                phone: '',
                imgCaptcha: '',
                phoneCaptcha: '',
                password: '',
                password2: '',
                invitationCode: '',
                isRead: false,

                promptMessage:'',
                imgCaptchaUrl: '',
                imgCaptchaToken: '',
                
            }
        },
        methods: {
            
            captcha() {
                let _this = this;
                axios('http://api.fxnn.net/captcha'+ '?random=' + Math.random())
                .then(function(res) {
                    _this.imgCaptchaUrl = res.data.inline
                    _this.imgCaptchaToken = res.data.token

                })
            },
            getsms() {
                let _this = this
                if(!this.phone){
                    this.promptMessage="请输入手机号"
                    return false
                }
                let myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})||(88[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
                if (!myreg.test(this.phone)) {
                    this.promptMessage="请输入正确的手机号"
                    this.phone = ''
                    return false
                }
                if(!this.imgCaptcha){
                    this.promptMessage="请输入图形验证码"
                    return false
                }

                let data = {
                    phrase: this.imgCaptcha,
                    token: this.imgCaptchaToken,
                    mobile: this.phone,
                    random: Math.random()
                }

                axios({
                    method: 'post',
                    url: 'http://api.fxnn.net/sms/getCode',
                    data: data
                })
                .then(function(res){
                    _this.$toast("短信验证码已发送至您填写的手机号，请注意查收")
                })
            },
            register() {
                let _this = this
                if(!this.phone){
                    this.promptMessage="请输入手机号"
                    return false
                }
                let myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})||(88[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
                if (!myreg.test(this.phone)) {
                    this.promptMessage="请输入正确的手机号"
                    this.phone = ''
                    return false
                }
                if(!this.imgCaptcha){
                    this.promptMessage="请输入图形验证码"
                    return false
                }
                if(!this.phoneCaptcha){
                    this.promptMessage="请输入手机验证码"
                    return false
                }
                if(!this.password || !this.password2) {
                    this.promptMessage="请输入密码和验证密码"
                    return false
                }
                if(this.password.length < 6) {
                    this.promptMessage="密码最少6位"
                    return false
                }
                if(this.password !== this.password2){
                    this.promptMessage="两次密码不一致"
                    return false
                }
                if (!this.isRead) {
                    this.promptMessage="请同意并阅读《社汇圈协议》"
                    return false
                }
                let data = {
                    mobile: this.phone,
                    password: this.password,
                    ycode: this.invitationCode,
                    code: this.phoneCaptcha,
                    password_confirmation: this.password2,
                    random: this.random
                }   

                axios({
                    method: 'post',
                    url: 'http://api.fxnn.net/login/regist',
                    data: data
                })
                .then(function(res) {
                    console.log(res)
                    if(!res.data.success) {
                        _this.promptMessage=res.data.errors.mobile[0]
                        return false
                    } else {
                        _this.$toast("注册成功")
                        setTimeout(function(){
                            _this.$router.push('/login')
                        }.bind(_this),3000)
                    }
                    
                    // 页面跳转到登陆页面
                })


            },
            confirmPassword() {
                if(this.password !== this.password2){
                    this.promptMessage="两次密码不一致"
                } else {
                    this.promptMessage=""
                }
            }
        },
        created() {
            this.captcha();
        }
    }
</script>


<style scoped>
    .register-view {
        height: 100%;
        padding: 0 30px;

    }
    .content-wrap .logo {
        height: 100px;
        margin-bottom: 40px;
    }
    .content-wrap .logo img {
        width: 100px;
    }
    .content-wrap .promptMessage {
        font-size: 13px;

        color: red;
    }
    .content-wrap {
        height: 100%;
        
        display: flex;
        flex-direction: column;
        justify-content: center;
    }
    .content-wrap > div {
        position: relative;
        height: 44px;
        /*border-bottom: 1px solid red;*/
    }
    .content-wrap input {
        border: none;
        border-bottom: 1px solid #ccc;
        width: 100%;
        height: 32px;
        margin-top: 8px;
        padding-left: 5px;
        outline: none;
    }
    .content-wrap .phone p {
        position: absolute;
        top: -35px;left:0; right: 0;
        text-align: center;
    }
    .content-wrap .imgCaptcha img {
        position: absolute;
        right: 0;bottom: 3px;
        border-radius: 3px;
        z-index: 9;
    }
    .content-wrap .phoneCaptcha button {
        position: absolute;
        right: 0;bottom: 3px;
        height: 30px;
        border: 1px solid #ddd;
        border-radius: 3px;
        background: #FFF54C;
        font-size: 12px;
        color: #666;
        letter-spacing: 1px;
        outline: none;
        z-index: 9;
    }
    .content-wrap .isRead {
        font-size: 12px;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #666;
    }
    .content-wrap .isRead input {
        width: auto;
        position: relative;
        top: -1px;left: -1px
    }
    .content-wrap  a {
        color: #aaa;
        font-size: 12px;
        text-decoration: none;
    }
    .content-wrap .register {
        margin: 15px 0;
    }
    .content-wrap .register button {
        width: 100%;
        height: 38px;
        border:none;
        background: #FFF54C;
        border-radius: 3px;
        outline: none;
    }

</style>